// pages/add-address/add-address.js
const common = require('../../../../../utils/common')
const request = require('../../../../../utils/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //省市区
        region: [],
        regionString: '',


        area_id: '',
        is_def: false,

        address_id: '', //编辑收货地址
        addressInfo: {},

        visible: false,
        value: [0, 0, 0],
        regionValue: [0, 0, 0], // 地址选择器省市区 最终 currentIndex
        // provinces: [], // 一级地址
        addressAll: null,
        // citys: [], // 二级地址
        // areas: [], // 三级地址
        xiaoquList: [],
        xiaoquIndex: -1,
        xiaoqu_name:'',
        xiaoqu:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            address_id: options.id
        })
        this.initData()
        this.getXiaoqu()
        this.getAreaList()
        const eventChannel = this.getOpenerEventChannel()
        if (eventChannel) {
            let _this = this
            // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
            eventChannel.on('acceptDataFromOpenerPage', function (data) {

                _this.setData({
                    mobile: data.data.telNumber,
                    name: data.data.userName,
                    address: data.data.detailInfo,
                    area_id: data.data.addressId,
                    area_name: data.data.provinceName + data.data.cityName + data.data.countyName
                })
            })
        }



    },
    onShow() {

    },
    initData() {
        let _this = this
        if (!this.data.address_id) {
            return
        }
        request._post('/api?method=user.getshipdetail', {
            id: this.data.address_id
        }).then(res => {
            let datas = res.data
          
            _this.setData({
                area_id: datas.area_id,
                area_name: datas.area_name,
                mobile: datas.mobile,
                is_def: datas.is_def == 1 ? true : false,
                name: datas.name,
                address: datas.address,
                xiaoqu: datas.xiaoqu,
                xiaoqu_name:datas.xiaoqu_name
            })
        })
    },
    // 获取所有区域
    getAreaList() {

        let _this = this

        request._post('/api?method=util.getallarea', {}).then(res => {
            console.log(res, 3333)
            _this.setData({
                addressAll: {
                    provinces: res.data.list,
                    citys: res.data.list1,
                    areas: res.data.list2
                }
            })
        })
    },

    // 子组件传值区域id
    sendData(e) {
        if (!e.detail.regionId) {
            return
        }
        this.setData({
            area_id: e.detail.regionId
        })
    },
    // 所在小区
    sendDataXiaoqu(e) {
        if (!e.detail.regionId) {
            return
        }
        this.setData({
            xiaoqu: e.detail.regionId
        })
    },
    // 输入框赋值
    inputWrite(e) {
        this.setData({
            [e.target.dataset.name]: e.detail.value
        })
    },
    // 设置默认地址
    switchChange(e) {
        let is_def = e.detail.value
        this.setData({
            is_def
        })
    },
    //提交
    submit() {
        let {
            mobile,
            name,
            area_id,
            address,

        } = this.data
        if (!name) {
            return getApp().toast('请输入您的真实姓名')
        }
        if (!this.data.mobile) {
            return getApp().toast('请输入您的手机号码')
        }
        if (!common.isMobile(this.data.mobile)) {
            return getApp().toast('请输入正确的手机号码')
        }

        if (!this.data.area_id) {
            return getApp().toast('请选择所在城市')
        }
        if (!this.data.xiaoqu) {
            return getApp().toast('请选择所在小区')
        }
        if (!this.data.address) {
            return getApp().toast('请输入详细地址')
        }
        request._post('/api?method=user.editship', {
            mobile,
            area_id,
            address,
            name,
            is_def: this.data.is_def ? 1 : 2,
            xiaoqu:this.data.xiaoqu
        }).then(res => {
            getApp().toast(res.msg)
            setTimeout(function () {
                wx.navigateBack({
                    delta: 1,
                })
            }, 1000)
        })
    },
    // 获取小区
    getXiaoqu() {
        let _this = this
        // if(wx.getStorageSync('xiaoquList')) {
        //     this.setData({
        //         xiaoquList:wx.getStorageSync('xiaoquList')
        //     })
        //     return
        // }
        request._post('/api?method=user.xiaoqu', {}, '', true).then(res => {
            this.setData({
                xiaoquList: res.data
            })
            // wx.setStorage({
            //     key: 'xiaoquList',
            //     data:res.data
            // })
        })
    },
    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            xiaoquIndex: e.detail.value,
            xiaoqu_name:this.data.xiaoquList[e.detail.value].name,
            xiaoqu:this.data.xiaoquList[e.detail.value].id,
        })
    },

    // 处理省市县联动逻辑 并保存 value
    cityChange(e) {
        var value = e.detail.value
        let {
            provinces,
            citys
        } = this.data
        var provinceNum = value[0]
        var cityNum = value[1]
        var areaNum = value[2]
        let address = this.data
        if (this.data.value[0] != provinceNum) {
            var id = provinces[provinceNum].id
            this.setData({
                value: [provinceNum, 0, 0],
                citys: address.citys[id],
                areas: address.areas[address.citys[id][0].id]
            })
        } else if (this.data.value[1] != cityNum) {
            var id = citys[cityNum].id
            this.setData({
                value: [provinceNum, cityNum, 0],
                areas: address.areas[citys[cityNum].id]
            })
        } else {
            this.setData({
                value: [provinceNum, cityNum, areaNum]
            })
        }
    },

    // /** 
    //  * 省市区
    //  */
    // bindRegionChange: function (e) {
    //     const region = e.detail.value;
    //     const regionString = region[0] + '-' + region[1] + '-' + region[2];
    //     this.setData({
    //         region: e.detail.value,
    //         regionString: regionString,
    //     })
    // },
})